#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "jsmn.c"


int strMayPositiveInt(char *str)
{
return strtol(str,NULL,0);
}

char *jsmn_chunk(char *str,jsmntok_t tok)
{
char *ss=malloc(sizeof(char)*(tok.end-tok.start+1));
int i;
for(i=tok.start; i<tok.end; i++) {
	ss[i-tok.start]=str[i];
}
ss[i-tok.start]='\0';
return ss;
}

int main()
{

int SQUAWK=1;
char *jsontext="\
id:1136155,genomealign:{chr:\"chr4\",start:29168689,stop:29170194,strand:\"-\",targetseq:\"AGGCTTCGATGCCCCTCCACACCCTCTTGATCTTCCCTGTGATGTCA-TCTGGAGCCCTGCTGCTTGCGG-TGGCCTATAAAGC---CTCCTAGTCTGGCTCCAAGGCCTGGCAGAGTCTTTCCCAG------GGAAAGCTA--CAAGCAGCAAACAGTCTGCATGGGTCATCCCCTTCA--CTCCCAGCTCAGAGCCCAGGCCAGGGGCCCCCAAG-------------AAAGGCTCTGGTGGAGAACCTGTGCATGAAGGCTGTCAACCAGTCCATAGGCAAGCCTGGCTGCCTCCAGCTGGGTCGACAGACAGGGGCTGGAGAAGGGG-AGAAGAGGAAAGTGAGGTTGCCTGCCCTGTCTCC TACCTGA-GGCTGAGGAAGGAGA----AGGGGATGCACTGTTGGGGAGGCAGCTGTAACTCAAAGCCTTAGCCTCTGTTCCCACGAAGGCAGGGCCATCAGGCACCAAAGGGATTCTGC CAGCATAGTGCTCCTGGACCAGTGATACACCCGGCACCCTGTCCTGGACACGCTGTTGGCCTGGATCTGAGCCCTGGTGGAGGTCAAAGCCACCTTTGGTTCTGCCATTGCTGCTGTGT GGAAGTTCACTCCTGCCTTTTCCTTTCCCTAGAGCCTCCACCACCCCGAGATCACATTTCTCACTGCCTTTTGTCTGCCCAGTTTCACCAGAAGTAGGCCTCTTCCTGACAGGCAGCTG CACCACTGCCTGGCGCTGTGCCCTTCCTTTGCTCTGCCCGCTGGAGACGGTGTTTGTCATGGGCCTGGTC---TGCAGGGATCCTGCTACAAAGGTGAAACCCAGGAGAGTGTGGAGTC CAGAGT-----GTTGCCAGGACCCAGGCACAGGCATTAGTGCCCGTTGGAGAAAACAGGGGAATCCC-GAAGAAATGGTGGGTCCTGGCCATCCGTGAGATCTTCCCAGGGCAGCTCCC CTCTGTGGAATCCAATCTGTCTTCCATCCTGCGTGGCCGAGGGCCAGGCTTCTCACTGGGCCTCTGCAGGAGGCTGCC--ATTTGTCCTGCCCACCTTCTTAGAAGCGAGACGGAGCAG ACCCATCT-GCTACTGCCCTTTCTATAATAACTAAAGTTAGCTGCCCTGGACTATTCACCCCCTAGTCTCAATTTAAGAAGATCCCCATGGCCACAGGGCCCCTGCCTGGGGGCTTGTC ACCTCCCCCACCTTCTTCCTGAGTCAT-TCCTGCAGCCTTGCTCCCTAACCTGCCCCACAGCCTTGCCT--GGATTTCTATCTCCCTGGCTTGGTGCCAGTTCCTCC---AAGTCGATG GCACCTCCCTCCCTCTCAACCACTTGAGCAAACTCCAAGACATCTTCTACCCCAACACCAGCAATTGTGCCAAGGGCCATTAGGCTCTCAGCATGACTATTTTTAGAGACCCCGTGTCT GTCACTGAAACCTTTTT-------TGTGGGAGACTATTCCTCCCATCTGCAACAGCTGCCCCTGCTGACTGCCCTTCTCTCCTCCCTCTCATCCCAGAGAAA---CAGGTCAGCTGGGA GCTTCTGCCCCCACTGCCTAGGGACCAACAGGGGCAGGAGGCAGTCACTGACCCCGAGA--CGTTTGCATCCTGCACAGCTAGAGATCCTTTATTAAAAGCACACTGTTGGTTTCTGCT CAGTTCTT\",queryseq:\"ACGCTATGACTCTCTGCTGTACACACGTGTTCTGAC-TATTATGAAAGTTTTGAGGTATACTCCTTGCCCCTTACCTATTAAACTAGCTACTGGCCTGG GCCAATGAACTGTCTTAGTGTTTCTCTGAAGCCTGGGACACCAGGCAGGTAAAAATCAATCCTGATTAGCTATCTCCTTTGTCCTTTCTACTTAGGACCACCAATTATACCTCCCCTGC ACTCTTCACCCTAAAGTTGTTGATGTCAGAGCTTTG--TGAAGGCTGATAACCAATCCAT-GGTAAGCCTGGA---CTCTAGC-------ACAGA--GGGACAGATGAATGGATGGAAG AAGCAAGGGATTTGTCCTATTAAAACTGTCACCTGAAGGCCAAGGAAGGGGGGACTAATGGACACACAGTGTAGGAAGCAGGCTTAACTCCAAGCCC-AGCTTCTATGCTCACCCAGGC AAGGTCACCAGGCAAAAGAAGGCCTTTCCTGAAGTCCCCTTCCTGG------GATACCCTC--------------------CTACTGGCAT-------AGCCATGTGGGAG----AAGC CACCTTTGACCCTGTCTTTGCTGCTGTACAGAAGGTTGGGGCTACCCTTTCCTCCTCATGAA--CTGCCCTGGCTCATCATATGACTTCTCACAGCC---------CCCA----CACC- --------------CCCGATGGTTGGCCATCTTCCTGCCTGGTGC-ACACTCTTACTTTGCCCTGTGTGCTTGCCAGCGCCTTTATCTTGACCCTGGGAAAGTAAAGGCATC------- ----GCTAAGCCCACATTAGCTT----CCTAGAGACACCAGCCACTGGGGCACAGACTTACACAGAGAAGACAGGAGGAGCAAATCCAGCAACCCCTGCAAGAACAGCAAGGAGTAG-- ACTCATTTGCTTAGGCCAGGATGGCTGCCCC---TGGACTAGATTCT---CCCCATTCATCATGGTCCAGAGGCAGCTTCCTGCTAGGGCCTTTGTAGGAGGGAGTAGGAAAAGCAACA CTCCCATTCTCAGAAGC----------CTCCTCATCCAGCCTCTGTTTTTTCCA-----GCTAAAACCAGTTGTTCTACACATCTCACATCTT-GCCAAAATTGCAGATG-TCCCCACT TCCAAA--------------------------TCACTGACATTCATAGAGTGCCATATACCCCAGCTTTGCTGCCTAACCT----CA-GGCCTGGCCTCAGGAGTGTGAGCTCCCTGGC TTGACTCCAGTTCCTCCCTGGAGCCAATGGCACCTCCCGCCCTC--GGCCACTTGAGCAAACGCCAAGACACCT---ACCCTGACATCAGCGATTACGCCAAGGGCCATTGGGCTCTCA GCATGTCTATTTTTAGAGACCCTGTGTCTATCACTGAGACATTTTTCCCCCACTGTGGAAAA-TATCCTTTCTCCTTTC--CATCTGCCCTT--------CTCTTCTCTTTTCACCCTC AGCCAAGAAAGGGCCTTGGTCACATCAGATGTTTTG--ACCATTGCCCAGAGACT-ACTTATGCAGGAAA-GGTCA-GGCCCCCGAGAGCCTACTGCACCCCACACAGCTAGAGCAGCTTGATCTCAAACACGAGCCTTGCTT-GGCTCAGTTCTT\"}";


int count=0;
int i,j;



int tokencount=0;
int discard=0, iskey=1;
int *a;
char *tmp;
jsmn_parser parser;
while(1)
	{
	tokencount+=50;
	jsmntok_t tokens[tokencount];
	for(i=0; i<tokencount; i++) {
		tokens[i].type=-1;
	}
	jsmn_init(&parser);
	if(jsmn_parse(&parser,jsontext,tokens,tokencount)==JSMN_ERROR_NOMEM) continue;
	// done parsing
	i=0;
	while(i<tokencount) {
		jsmntok_t t2=tokens[i];
		switch(t2.type)
		{
		case -1:
			i=tokencount;
			break;
		case JSMN_OBJECT:
			iskey=1;
			printf("{");
			for(j=0; j<t2.size; j++) {
				i++;
				jsmntok_t t3=tokens[i];
				switch(t3.type) {
				case JSMN_STRING:
					if(discard) {
						discard=0;
						iskey=1;
					} else {
						printf("\"%s\"",jsmn_chunk(jsontext,t3));
						if(iskey) printf(":");
						else printf(",");
						iskey=iskey==1?0:1;
					}
					break;
				case JSMN_PRIMITIVE:
					// unquoted key
					tmp=jsmn_chunk(jsontext,t3);
					if(strcmp(tmp,"targetseq")==0  || strcmp(tmp,"queryseq")==0) {
						discard=1;
						iskey=0;
					} else {
						printf("%s",tmp);
						if(iskey) printf(":");
						else printf(",");
						iskey=iskey==1?0:1;
					}
					break;
				}
			}
			printf("},");
			i++;
			break;
		case JSMN_ARRAY:
			i+=t2.size;
			iskey=1;
			break;
		case JSMN_STRING:
			if(discard) {
				discard=0;
				iskey=1;
			} else {
				printf("\"%s\"",jsmn_chunk(jsontext,t2));
				if(iskey) printf(":");
				else printf(",");
				iskey=iskey==1?0:1;
			}
			i++;
			break;
		case JSMN_PRIMITIVE:
			// unquoted key
			tmp=jsmn_chunk(jsontext,t2);
			printf("%s",tmp);
			if(iskey) printf(":");
			else printf(",");
			iskey=iskey==1?0:1;
			i++;
			break;
		}
	}
	break;
}
return 1;
}
